const nodemailer = require('nodemailer');
const smtpTransport = require('nodemailer-smtp-transport');

  function emailTo(email,subject,text,html,callback) {
  // 开启一个 SMTP 连接池
      var transporter = nodemailer.createTransport(smtpTransport({
          host: "smtp.qq.com",//主机
          secure: true, // 使用 SSL
          secureConnection: true, // 使用 SSL
          port: 465, // SMTP 端口
          auth: {
             user: "562868736@qq.com",
             pass: "vvbieapitcszbfec" //授权码,通过QQ获取  
 
         }
     }));
     var mailOptions = {
         from: "562868736@qq.com", // 发送者  
         to: email, // 接受者,可以同时发送多个,以逗号隔开  
         subject: subject, // 标题 
     };
     if(text != undefined)
         {
              mailOptions.text =text;// 文本  
          }
          if(html != undefined)
          {
              mailOptions.html =html;// html  
          }
        
     var result = {
         httpCode: 200,
         message: '发送成功!',
         data: [],
     }
     try {
         transporter.sendMail(mailOptions, function (err, info) {
             if (err) {
                 result.httpCode = 500;
                result.message = err;
                //callback(result);
                 return result;
             }
             callback(result);
         });
     } catch (err) {
         result.httpCode = 500;
         result.message = err;
         return result;
     }
     transporter.close(); // 如果没用，关闭连接池
     
 }
 module.exports = {
    emailTo
}
